package org.sundatagroup.aurora.show.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import org.sundatagroup.aurora.show.entity.asset.Asset;
import org.sundatagroup.aurora.show.entity.model.AssetModel;

/**
 * <p>封装{@link Asset}的数据操作的DAO层
 * </p>
 * 
 * @author CharlesChen
 * @date 2018-04-24
 * @since 0.1.1
 */
@Repository
public class AssetDao {
    @Autowired
    MongoTemplate mongoOps;

    private final static Logger LOGGER = LoggerFactory.getLogger(AssetDao.class);
    /**
     * 插入操作：插入一个
     * @param am
     */
    public void add(AssetModel am) {
        mongoOps.insert(am);
    }

    /**
     * 查找操作：利用Name和type找到目标{@link AssetModel}
     * @param name
     * @param type
     * @return
     */
    public AssetModel findByNameAndType(String name, String type) {
        AssetModel am;
        Query query = Query.query(Criteria.where("name").is(name).and("type").is(type));
        if ((am = mongoOps.findOne(query, AssetModel.class)) == null) {
            LOGGER.error("利用name和type查询Asset失败，请检查！");
            return null;
        } else {
            return am;
        }
    }

    /**
     * 查找操作：统计所有{@link AssetModel}数量
     * @return
     */
    public int count() {
        return (int) mongoOps.count(new Query(), AssetModel.class);
    }
}
